import { useNodeUtil } from '@/utils/nodeUtils'
import type { Router } from 'vue-router'
import { useStatusUtil } from '@/utils/statusUtils'

export function createRouterGuard(router: Router) {
  const statusUtil = useStatusUtil()
  const util = useNodeUtil()

  router.beforeEach(async (to, from, next) => {
    util.loadingStart()
    if (to.name !== 'Login_page' && !statusUtil.loginStatus()) {
      next({ name: 'Login_page' })
    } else {
      next()
    }
  })

  router.afterEach((to, _, faiulure) => {
    util.loadingStop()
  })

  router.onError((err) => {
    console.error(err, '路由错误')
  })
}
